﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HTSManager
{
    public partial class Main : Form
    {
        Platform platform;
        Securities securities;
        UserInfo userInfo;
        List<UserInfo> userInfoList;

        public Main()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            platform = new Platform();
            securities = new Securities();
            userInfoList = new List<UserInfo>();
            userInfo = new UserInfo(PlatformType.MUG, SettingTarketType.PLATFORM);

            // 최초 targetPlatform 가져오기
            switch (userInfo.PlatformType)
            {
                case PlatformType.MUG:
                    rbMug.Checked = true;
                    break;
                case PlatformType.TX:
                    rbTX.Checked = true;
                    break;
                case PlatformType.QTRADING:
                    rbQtrading.Checked = true;
                    break;
                case PlatformType.FORMDE:
                    rbFormDe.Checked = true;
                    break;
                default:
                    break;
            }

            rbLocalSave.Checked = true;
            // 설정 데이터 가져오기
            LoadSettingData();
            LoadSecuritySetting();

        }

        private void rbMug_CheckedChanged(object sender, EventArgs e)
        {
            if (rbMug.Checked == true)
            {
                userInfo.PlatformType = PlatformType.MUG;
                rbEmployee.Enabled = true;
                LoadSettingData();
            }
            else
                rbEmployee.Enabled = false;
        }

        private void rbTX_CheckedChanged(object sender, EventArgs e)
        {
            if (rbTX.Checked == true)
            {
                userInfo.PlatformType = PlatformType.TX;
                LoadSettingData();
            }
        }

        private void rbQtrading_CheckedChanged(object sender, EventArgs e)
        {
            if (rbQtrading.Checked == true)
            {
                userInfo.PlatformType = PlatformType.QTRADING;
                LoadSettingData();
            }
        }

        private void rbFormDe_CheckedChanged(object sender, EventArgs e)
        {
            if (rbFormDe.Checked == true)
            {
                userInfo.PlatformType = PlatformType.FORMDE;
                rbCustomer.Enabled = false;
                rbEmployee.Enabled = true;
                rbOperationServer.Enabled = false;
                tbPassword.Enabled = false;
                tbCertPassword.Enabled = false;
                cbMarketInquiry.Enabled = false;
                LoadSettingData();
            }
            else
            {
                rbCustomer.Enabled = true;
                rbEmployee.Enabled = false;
                rbOperationServer.Enabled = true;
                tbPassword.Enabled = true;
                tbCertPassword.Enabled = true;
                cbMarketInquiry.Enabled = true;
            }
        }

        private void LoadSettingData()
        {
            // 사용자 정보 가져오기
            userInfo.GetUserInfo();
            tbServer.Text = userInfo.ServerIP;
            tbUserId.Text = userInfo.UserID;
            tbPassword.Text = userInfo.UserPassword;
            tbCertPassword.Text = userInfo.UserCertPassword;
            cbMarketInquiry.Checked = userInfo.IsMarketInquary;

            // 고객용/직원용 구분
            if (userInfo.ServerType == ServerType.CUSTOMER)
                rbCustomer.Checked = true;
            if (userInfo.ServerType == ServerType.EMPOLYEE)
                rbEmployee.Checked = true;

            // 운영용/테스트용 구분
            if (userInfo.ConnectionServer == ConnectionServerType.OPERATION)
                rbOperationServer.Checked = true;
            if (userInfo.ConnectionServer == ConnectionServerType.TEST)
                rbTestServer.Checked = true;
        }

        private void SaveSettingData()
        {
            // 사용자 정보 저장하기
            userInfo.ServerIP = tbServer.Text;
            userInfo.UserID = tbUserId.Text;
            userInfo.UserPassword = tbPassword.Text;
            userInfo.UserCertPassword = tbCertPassword.Text;
            userInfo.IsMarketInquary = cbMarketInquiry.Checked;

            // 고객용/직원용 구분
            if (rbCustomer.Checked == true)
                userInfo.ServerType = ServerType.CUSTOMER;
            if (rbEmployee.Checked == true)
                userInfo.ServerType = ServerType.EMPOLYEE;

            // 운영용/테스트용 구분
            if (rbOperationServer.Checked == true)
                userInfo.ConnectionServer = ConnectionServerType.OPERATION;
            if (rbTestServer.Checked == true)
                userInfo.ConnectionServer = ConnectionServerType.TEST;
        }

        private void LoadSecuritySetting()
        {
            // 방화벽 체크
            if (securities.isCheckFirewall == true)
                cbFirewall.Checked = true;
            else
                cbFirewall.Checked = false;

            // 키보드보안 체크
            if (securities.isCheckKeySecurity == true)
                cbKeySecurity.Checked = true;
            else
                cbKeySecurity.Checked = false;

            // AOS 체크
            if (securities.isCheckAOS == true)
                cbAOS.Checked = true;
            else
                cbAOS.Checked = false;
        }

        private void cbForgery_CheckedChanged(object sender, EventArgs e)
        {
            /*
            if (cbForgery.Checked == false)
                securities.isCheckForgery = false;
            else
                securities.isCheckForgery = true;            
             * */
        }

        private void cbFirewall_CheckedChanged(object sender, EventArgs e)
        {
            if (cbFirewall.Checked == false)
                securities.isCheckFirewall = false;
            else
                securities.isCheckFirewall = true;
        }

        private void cbKeySecurity_CheckedChanged(object sender, EventArgs e)
        {
            if (cbKeySecurity.Checked == false)
                securities.isCheckKeySecurity = false;
            else
                securities.isCheckKeySecurity = true;
        }

        private void cbAOS_CheckedChanged(object sender, EventArgs e)
        {
            if (cbAOS.Checked == false)
                securities.isCheckAOS = false;
            else
                securities.isCheckAOS = true;
        }

        private void btRecover_Click(object sender, EventArgs e)
        {

        }

        private void rbTestServer_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rbOperationServer_CheckedChanged(object sender, EventArgs e)
        {
            if (rbOperationServer.Checked == true)
            {
                cbServerInfo.Enabled = false;
                tbServer.Enabled = false;
            }
            else
            {
                cbServerInfo.Enabled = true;
                tbServer.Enabled = true;
            }
        }

        private void btExecute_Click(object sender, EventArgs e)
        {
            string programPath = userInfo.strExecuteFilePath;
            Process.Start(programPath, " /U" + tbUserId.Text + " /P" + tbPassword.Text + " /C" + tbCertPassword.Text);
        }

        private void rbLocalSave_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rbManagerSave_CheckedChanged(object sender, EventArgs e)
        {
            /*
            if (rbManagerSave.Checked == true)
            {
                cbUserInfo.Enabled = true;
                cbServerInfo.Enabled = true;
            }
            else
            {
                cbUserInfo.Enabled = false;
                cbServerInfo.Enabled = false;
            }
            */
        }

        private void btInsertUserInfo_Click(object sender, EventArgs e)
        {
            SaveSettingData();
            userInfoList.Add(userInfo);
            cbUserInfo.Items.Add( userInfo.UserID);
            //cbUserInfo.Items.Insert(0, userInfo.UserID);
            //cbUserInfo.SelectedIndex = 0;
        }

        private void cbUserInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            userInfo = userInfoList.ElementAt(cbUserInfo.SelectedIndex);
            LoadSettingData();
        }
    }
}
